﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Physis.Client.Models.Sponsor {

    public class SponsorTypeModel : Configuration.ConfigurationItemModel {
                
        #region Private Properties

        private Core.Sponsor.SponsorType sponsorType = null;

        #endregion


        #region Public Properties

        public override Int64 ConfigurationItemId { set { SponsorTypeId = value; } }

        public override Core.CoreConfigurationObject ConfigurationItem { get { return sponsorType; } }


        public Int64 SponsorTypeId {

            set {

                sponsorType = Application.Core.Sponsor.SponsorType (value);

                if (sponsorType == null) {

                    sponsorType = new Core.Sponsor.SponsorType (Application);

                }

            }

        }

        public Core.Sponsor.SponsorType SponsorType { get { return sponsorType; } set { sponsorType = value; } }

        #endregion 


        #region Constructors

        public SponsorTypeModel (Int64 forSponsorTypeId) {

            SponsorTypeId = forSponsorTypeId;

            return;

        }

        public SponsorTypeModel (System.Collections.Specialized.NameValueCollection form) {

            UpdateValues (form);

            return;

        }

        #endregion 


        #region Public Methods

        public override void UpdateValues (System.Collections.Specialized.NameValueCollection form) {

            base.UpdateValues (form);

            
            // BASE RELOADS THE ORIGINAL OBJECT TYPE

            SponsorType.AllowedParents.Clear ();

            String[] allowedParentIds = Data.Readers.FormReader.StringFromForm (form, "AllowedParent").Split (',');

            foreach (String currentAllowedParentId in allowedParentIds) {

                Int64 allowedParentId = 0;

                if (Int64.TryParse (currentAllowedParentId, out allowedParentId)) {

                    Core.Sponsor.SponsorTypeAllowedParent allowedParent = new Core.Sponsor.SponsorTypeAllowedParent (Application);

                    allowedParent.SponsorTypeId = SponsorType.Id;

                    allowedParent.ParentSponsorTypeId = allowedParentId;

                    sponsorType.AllowedParents.Add (allowedParent);

                }

            }


            return;

        }

        #endregion 

    }

}